package com.echoesnet.echoesshortvideo.views.adapters;

import android.app.Activity;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.echoesnet.echoesshortvideo.AppController;
import com.echoesnet.echoesshortvideo.Persistence.TinyVideoInfoEntity;
import com.echoesnet.echoesshortvideo.R;
import com.echoesnet.echoesshortvideo.Utils.CommonUtils;
import com.echoesnet.echoesshortvideo.Utils.Image.GlideApp;
import com.echoesnet.echoesshortvideo.databinding.ItemVideoListBinding;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;

import java.util.List;

public class VideoListAdapter extends UltimateViewAdapter<UltimateRecyclerviewViewHolder> implements View.OnClickListener {
    private String TAG = VideoListAdapter.class.getSimpleName();

    private static final int VIEW_TYPE_VIDEO = 1000;
    private Activity mAct;
    private List<TinyVideoInfoEntity> dataList;

    private int itemW, itemH;

    public VideoListAdapter(Activity act, List<TinyVideoInfoEntity> list) {
        this.mAct = act;
        this.dataList = list;
//        itemW = (CommonUtils.getScreenWidth(mAct)-CommonUtils.dp2px(mAct, 1)) / 2;
        itemW = CommonUtils.getScreenWidth(mAct) / 2;
            //<!-- 374px * 480 px -->
            //<!-- 高是宽的1.28倍 -->
        itemH = (int)(itemW * 1.28);
//        itemH = 50;
    }

    public void setDataList(List<TinyVideoInfoEntity> dataList) {
        this.dataList = dataList;
    }

    // 复写 UltimateViewAdapter 增加自己类型------------------------------------------------------
    @Override
    public UltimateRecyclerviewViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(VIEW_TYPE_VIDEO == viewType){
            ItemVideoListBinding binding = DataBindingUtil.inflate(
                    LayoutInflater.from(parent.getContext()),
                    R.layout.item_video_list,
                    parent,
                    false);

            binding.getRoot().setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,itemH));

            binding.ivAvatarLeft.setOnClickListener(this);
            binding.tvNickNameLeft.setOnClickListener(this);
            binding.ivCoverImgLeft.setOnClickListener(this);
            binding.ivAvatarRight.setOnClickListener(this);
            binding.tvNickNameRight.setOnClickListener(this);
            binding.ivCoverImgRight.setOnClickListener(this);

            BindingHolder holder = new BindingHolder(binding.getRoot());
            holder.setBinding(binding);

            return holder;
        }
        return super.onCreateViewHolder(parent, viewType);
    }

    @Override
    public int getItemViewType(int position) {
        if(VIEW_TYPES.NORMAL != super.getItemViewType(position)){
            return super.getItemViewType(position);
        }else {
//            int fixPosition = position + (getItemCount() - getAdapterItemCount()); // 当前位置 补偿 总item与内容item的差值
//            if(enableLoadMore())
//                fixPosition -=1;    //footer 存在 刚才就加多了

            return VIEW_TYPE_VIDEO;
        }
    }
    // 无用
    @Override
    public UltimateRecyclerviewViewHolder onCreateViewHolder(ViewGroup parent) {
        return null;
    }
    // END----------------- 复写 UltimateViewAdapter 增加自己类型------------------------------------------------------


    public static class BindingHolder extends UltimateRecyclerviewViewHolder {
        private ViewDataBinding binding;
        public BindingHolder(View itemView) {
            super(itemView);
        }
        public ViewDataBinding getBinding() {
            return binding;
        }
        public void setBinding(ViewDataBinding binding) {
            this.binding = binding;
        }
    }

    @Override
    public void onBindViewHolder(UltimateRecyclerviewViewHolder viewHolder, int position) {
        if (getItemViewType(position) == VIEW_TYPE_VIDEO) {
            BindingHolder bindingHolder = (BindingHolder) viewHolder;
            bindDefaultTileItem(position, bindingHolder);
        }
//        else if (getItemViewType(position) == VIEW_TYPE_GALLERY) {
//            final GalleryViewHolder holder = (GalleryViewHolder) viewHolder;
//            bindLoadingGallery(holder);
//        }
    }


    private void bindDefaultTileItem(int position, BindingHolder holder) {
        int l = position *2;
        int r = position *2 + 1;
        TinyVideoInfoEntity videoInfoLeft = dataList.get(l);
        TinyVideoInfoEntity videoInfoRight = r >= dataList.size() ? null : dataList.get(r);
        ItemVideoListBinding binding = (ItemVideoListBinding) holder.getBinding();

        //l
        if (TextUtils.isEmpty(videoInfoLeft.getNickName())) {
            binding.tvNickNameLeft.setText(videoInfoLeft.getUserId());
        } else {
            binding.tvNickNameLeft.setText(videoInfoLeft.getNickName());
        }

        binding.tvTimeLeft.setText(videoInfoLeft.getTimeToNow());
        binding.tvTitleLeft.setText(videoInfoLeft.getContent());


        //给listen加值，在 onCreateViewHolder 里；
        binding.ivAvatarLeft.setTag(R.id.viewTagFirst,videoInfoLeft);
        binding.tvNickNameLeft.setTag(R.id.viewTagFirst,videoInfoLeft);
        binding.ivCoverImgLeft.setTag(R.id.viewTagFirst,videoInfoLeft);


        GlideApp.with(AppController.getInstance())
                .asBitmap()
                .placeholder(R.drawable.bg_live_default)
                .centerCrop()
                .load(videoInfoLeft.getThumbnails())
                .into(binding.ivCoverImgLeft);

        GlideApp.with(AppController.getInstance())
                .load(videoInfoLeft.getHeadPic())
                .placeholder(R.drawable.default_120px)
                .centerCrop()
                .circleCrop()
                .into(binding.ivAvatarLeft);

        //r
        if(null == videoInfoRight){
            binding.flItemRight.setVisibility(View.INVISIBLE);
        } else {
            binding.flItemRight.setVisibility(View.VISIBLE);
            if (TextUtils.isEmpty(videoInfoRight.getNickName())) {
                binding.tvNickNameRight.setText(videoInfoRight.getUserId());
            } else {
                binding.tvNickNameRight.setText(videoInfoRight.getNickName());
            }

            binding.tvTimeRight.setText(videoInfoRight.getTimeToNow());
            binding.tvTitleRight.setText(videoInfoRight.getContent());


            //给listen加值，在 onCreateViewHolder 里；
            binding.ivAvatarRight.setTag(R.id.viewTagFirst,videoInfoRight);
            binding.tvNickNameRight.setTag(R.id.viewTagFirst,videoInfoRight);
            binding.ivCoverImgRight.setTag(R.id.viewTagFirst,videoInfoRight);


            GlideApp.with(AppController.getInstance())
                    .asBitmap()
                    .placeholder(R.drawable.bg_live_default)
                    .centerCrop()
                    .load(videoInfoRight.getThumbnails())
                    .into(binding.ivCoverImgRight);

            GlideApp.with(AppController.getInstance())
                    .load(videoInfoRight.getHeadPic())
                    .placeholder(R.drawable.default_120px)
                    .centerCrop()
                    .circleCrop()
                    .into(binding.ivAvatarRight);
        }



    }



    @Override
    public long getItemId(int position) {
        return position;
    }



    private ItemClickListener onItemClickListener;
    public void setOnItemClickListener(ItemClickListener l) {
        this.onItemClickListener = l;
    }

    public interface ItemClickListener{
        void onClick(View v, TinyVideoInfoEntity tag, int position);
    }
    @Override
    public void onClick(View v) {
        if (onItemClickListener != null) {
            onItemClickListener.onClick(v,(TinyVideoInfoEntity)v.getTag(R.id.viewTagFirst), dataList.indexOf(v.getTag(R.id.viewTagFirst)));
        }
    }



    @Override
    public UltimateRecyclerviewViewHolder newFooterHolder(View view) {
        return new UltimateRecyclerviewViewHolder(view);
    }

    @Override
    public UltimateRecyclerviewViewHolder newHeaderHolder(View view) {
        return null;
    }


    @Override
    public int getAdapterItemCount() {
        int len = 0;
        if(dataList.size() % 2 == 0){
            len = dataList.size()/2;
        }else {
            len = dataList.size()/2 + 1;
        }
        return len;
    }

    @Override
    public long generateHeaderId(int position) {
        return 0;
    }


    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        return null;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {

    }



}
